package com.net.xpay.stat.scheduler;


import com.net.xpay.stat.task.PartnerSettleDailyTask;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.common.service.PartnerSettleService;
import com.net.xpay.common.service.PartnerWithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 2019/7/15 16:28

 * 结算合伙人佣金
 */
@Slf4j
@Component
public class PartnerSettleScheduler {

    @Autowired
    private PartnerSettleService partnerSettleService;

    @Autowired
    private PartnerSettleDailyTask partnerSettleDailyTask;

    @Autowired
    private PartnerWithdrawService partnerWithdrawService;

    @Autowired
    private NoticeHelper noticeHelper;

    /**
     * 月结 每月1号 3点 结算合伙人佣金
     */
    @Scheduled(cron = "0 0 3 1 * ?")
    public void settleMonthPartnerBenefit() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule settleMonthPartnerBenefit|now={}", LocalDateTime.now());
            partnerSettleService.settleMonthPartnerBenefit();
            long t2 = System.currentTimeMillis();
            log.info("end schedule settleMonthPartnerBenefit|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("合伙人月佣金结算记录生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("settleMonthPartnerBenefit error", e);
            noticeHelper.sendSystemMonitorNotice("合伙人月佣金结算失败|settleMonthPartnerBenefit error", "text");
        }
    }

    /**
     * 日结 3点30分 结算合伙人佣金
     */
    @Scheduled(cron = "0 30 3 * * ?")
    public void settleDayPartnerBenefit() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule settleDayPartnerBenefit|now={}", LocalDateTime.now());
            partnerSettleService.settleDayPartnerBenefit();
            long t2 = System.currentTimeMillis();
            log.info("end schedule settleDayPartnerBenefit|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("合伙人日佣金结算记录生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("settleDayPartnerBenefit error", e);
            noticeHelper.sendSystemMonitorNotice("合伙人日佣金结算失败|settleDayPartnerBenefit error", "text");
        }
    }

    /**
     * 每天6点 提现合伙人佣金
     */
    @Scheduled(cron = "0 0 6 * * ?")
    public void createPartnerWithdrawRecordBatch() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule createPartnerWithdrawRecordBatch|now={}", LocalDateTime.now());
            partnerWithdrawService.createPartnerWithdrawBatch();
            long t2 = System.currentTimeMillis();
            log.info("end schedule createPartnerWithdrawRecordBatch|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("合伙人余额提现记录生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("createPartnerWithdrawRecordBatch error", e);
            noticeHelper.sendSystemMonitorNotice("合伙人余额提现失败|createPartnerWithdrawRecordBatch error", "text");
        }
    }

    /**
     * 合伙人佣金统计
     */
    @Scheduled(cron = "0 30 0 * * *")
    public void doPartnerSettleDailyTask() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule doPartnerSettleDailyTask|now={}", LocalDateTime.now());
            partnerSettleDailyTask.startDailyWork();
            long t2 = System.currentTimeMillis();
            log.info("end schedule doPartnerSettleDailyTask|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("合伙人佣金统计完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("doPartnerSettleDailyTask error", e);
            noticeHelper.sendSystemMonitorNotice("合伙人佣金统计失败|doPartnerSettleDailyTask error", "text");
        }
    }


}
